<div>
<script>
    function cerrar()
    {
        $('#modal_fecha').modal('hide');
    }
    
    function moredetails(value, title)
    {
        $('#error_msj').html('');
        $('#table_team_game').html('');
        $('#title_date').html("(" + title + ")");
        
        var data_ajax={
                        type: 'POST',
                        url: "<?php echo base_url(); ?>user/date_of_user",
                        data: { fecha: value, gpo:  $("#group_").val() },
                        success: function( data ) {
                                                    if(data != 0)
                                                    {	
                                                       //good
                                                       var cant = 0;
                                                       var insert;
                                                       var local;
                                                       var localImg;
                                                       var visita;
                                                       var visitaImg;
                                                       var resulUsr;
                                                       var resulGame;
                                                       var willCard = -1;
                                                       
                                                       $.each(data, function(k,v)
                                                                    {
                                                                        cant++;
                                                                        //Datos de game
                                                                        $.each(v, function(i,j)
                                                                                   {
                                                                                        switch(i)
                                                                                        {
                                                                                            case "localName":
                                                                                                local = j;
                                                                                                break;
                                                                                            
                                                                                            case "localImage":
                                                                                                localImg = j;
                                                                                                break;
                                                                                            
                                                                                            case "visitName":
                                                                                                visita = j;
                                                                                                break;
                                                                                            
                                                                                            case "visitImage":
                                                                                                visitaImg = j;
                                                                                                break;
                                                                                            
                                                                                            case "resultUsr":
                                                                                                resulUsr = j;
                                                                                                break;
                                                                                            
                                                                                            case "resultFin":
                                                                                                resulGame = j;
                                                                                                break;
                                                                                            
                                                                                            case "willCard":
                                                                                                if(j != null)
                                                                                                    willCard = j;
                                                                                                    else
                                                                                                    willCard = -2;
                                                                                                    break;
                                                                                            
                                                                                        }
                                                                                   }
                                                                                  );
                                                                        //
                                                                       insert  = '<td width="25"><img src="<?php echo base_url();?>assets/img/escudos/'+localImg+'" width="25" height="25"></td>';
                                                                       insert += '<td>'+local+'</td>';
                                                                       insert += '<td>'+visita+'</td>';
                                                                       insert += '<td width="25"><img src="<?php echo base_url();?>assets/img/escudos/'+visitaImg+'" width="25" height="25"></td>';
                                                                       insert += '<td style="text-align: center">';
                                                                       if(resulUsr == 1)
                                                                       {
                                                                            insert += "L";
                                                                       }
                                                                       else
                                                                       {
                                                                            if(resulUsr == 2)
                                                                            {
                                                                                insert += "V";
                                                                            }
                                                                            else
                                                                            {
                                                                                if(resulUsr == 0)
                                                                                {
                                                                                    insert += "E";
                                                                                }
                                                                                else
                                                                                {
                                                                                    insert += "-";    
                                                                                }
                                                                            }
                                                                       }

                                                                       if(willCard != -2)
                                                                       {
                                                                            switch(willCard)
                                                                            {
                                                                                case "1":
                                                                                    insert += "/L";
                                                                                    break;
                                                                                
                                                                                case "2":
                                                                                    insert += "/V";
                                                                                    break;
                                                                                
                                                                                case "0":
                                                                                    insert += "/E";
                                                                                    break;
                                                                            }
                                                                       }
                                                                       
                                                                       insert += '</td>';
                                                                       insert += resulGame == 1 ? '<td style="text-align: center">L</td>' : resulGame == 2 ? '<td style="text-align: center">V</td>' : resulGame == 0 ? '<td style="text-align: center">E</td>' : '<td style="text-align: center">-</td>';
                                                                       if(resulGame != -1)
                                                                       {
                                                                            if(resulUsr == resulGame || willCard == resulGame)
                                                                            {
                                                                                 insert += '<td width="25" style="text-align: center; background: #468847;"><i class="glyphicon glyphicon-ok-sign"></i></td>';
                                                                            }
                                                                            else
                                                                            {
                                                                                 insert += '<td width="25" style="text-align: center; background: #b94a48;"><i class="glyphicon glyphicon-remove-sign"></i></td>';
                                                                            }
                                                                       }
                                                                       else
                                                                       {
                                                                            insert += '<td width="25" style="text-align: center; background: #F89406;"><i class="glyphicon glyphicon-minus-sign"></i></td>';
                                                                       }
                                                                       
                                                                       $('#table_team > tbody:last').append('<tr>'+insert+'</tr>');
                                                                    }
                                                                   );
                                                       
                                                       if( cant == 0 )
                                                       {
                                                            $('#table_team').css("display", "none");
                                                            $('#error_msj').html('<strong>No se registro jugada.</strong>');
                                                       }
                                                       else
                                                       {
                                                            $('#error_msj').html('');
                                                       }
                                                    }
                                                    else
                                                    {
                                                        //$('#table_team').css("display", "none");
                                                       $('#error_msj').html('<strong>No se registro jugada.</strong>');
                                                    }
                                                  },
                        error: function(){
                                $('#table_team').css("display", "none");
                                $('#error_msj').html('<strong>Ocurrio un error, intente nuevamente la acción.</strong>');
                            },
                        dataType: 'json'
                        };
			$.ajax(data_ajax);
        
			
    }
    
    function setResult(gpoId, name)
    {
        $("#group_").val(gpoId);
        $('#games tbody').html('');
        $("#spanNameR").html(name);
        
        var data_ajax={
                        type: 'POST',
                        url: "<?php echo base_url(); ?>user/get_result_by_group",
                        data: { gpo: gpoId },
                        success: function( data ) {
                                                    if(data != 0)
                                                    {
                                                        var puntos = 0;
                                                        var fila = "";
                                                        var id = "";
                                                        var name = "";
                                                         $.each(data, function(k,v){
                                                            fila = "<tr>";
                                                            $.each(v, function(i,j){
                                                               switch (i) {
                                                                case 'dtId':
                                                                    id = j;
                                                                    break;
                                                                case 'dtDescription':
                                                                    fila+= "<td>"+j+"</td>";
                                                                    name = j;
                                                                    break;
                                                                case 'points':
                                                                   fila+= "<td>"+j+"</td>";
                                                                   puntos += j;
                                                                    break;
                                                               }
                                                            });
                                                            fila += "<td style=\"text-align: center\">";
                                                            fila += "<i class=\"fa fa-search\" style=\"cursor:pointer;\" title=\"ver ficha\" onclick=\"moredetails('" + id + "','" + name + "')\" ></i>";
                                                            fila += "</td>";
                                                            fila += "</tr>";
                                                            $('#games tbody').append(fila);
                                                         });
                                                         
                                                        var fila = "<tr>";
                                                        fila += "<td style=\"text-align: right;\"><strong>Total:</strong></td>";
                                                        fila += "<td style=\"text-align: center; background-color: #51A351;\"><strong>" + puntos + "</strong></td>";
                                                        fila += "<td style=\"text-align: center\">-</td>";
                                                        fila += "</tr>";
                                                        $('#games tbody').append(fila);
                                                    }
                        },
                        error: function(){
                                $('#table_team').css("display", "none");
                                $('#error_msj').html('<strong>Ocurrio un error, intente nuevamente la acción.</strong>');
                            },
                        dataType: 'json'
                        };
			$.ajax(data_ajax);
                        
    }
    
        $("#addGroupBtn").click(function(){
	    //remove class active
	    $(".side-nav li").removeClass("active");
	    //add class active to selected element
	    //$(this).addClass('active');
	    $("#groupsOption").addClass('active');
	    $("#content_container").html('');
	    $("#content_container").html("");
	    var data_ajax={
		    type: 'POST',
		    url: "groups",
		    data: { },
		    success: function( data ) {
						$("#content_container").html(data);
					      },
		    error: function(data){$("#content_container").html(data);},
		    dataType: 'json'
		    };
	    $.ajax(data_ajax);
	});
</script>


<div class="row">
            <div class="col-md-6">
               
               <!--#region resultados -->
                <section class="widget">
                    <header>
                        <h4>
                            <i class="fa fa-signal"></i>
                            Tus
                            <small>
                                resultados
                            </small>
                        </h4>
                    </header>
                    <div class="body text-align-center" >
                        
                        <?php
                            if($this->session->userdata('logged_in'))
                            {
                                if($results == 0 && count($groups) <= 0)
                                {
                                    ?>
                                    <div class="alert alert-error">
                                        No tienes grupos asociados aun.
                                    </div>
                                    <?php
                                    echo '<button id="addGroupBtn" style="margin-left: 15px;" type="button" class="btn btn-success">
                                            Crear un grupo e invitar a mis amigos
                                          </button>';
                                }
                                else
                                {
                                    if($results == 0)
                                    {
                                        ?>
                                            <div class="alert alert-error">
                                                No hay resultados cargados aun.
                                            </div>
                                        <?php
                                    }
                                    else
                                    {
                                    ?>
                                    
                                    <div>
                                        <table>
                                            <tr>
                                                <td>
                                                    <label for="article-category" class="control-label" style="padding-right: 15px; float: left;">Grupo:
                                                </td>
                                                <td>
                                                    <div class="controls form-group" style="float: left;">
                                                        <select id="group_" class="input-large" data-style="btn-warning" class="selectpicker" style="display: none;">
                                                            <?php
                                                                foreach($groups as $value)
                                                                {
                                                                    echo '<option value="'.$value['id'].'">"'.$value['name'].'"</option>';
                                                                }
                                                            ?>
                                                        </select>
                                                        <div class="btn-group bootstrap-select" style="padding-top: 15px;">
                                                            <button class="btn dropdown-toggle clearfix btn-success" data-toggle="dropdown" id="group_">
                                                                <span class="filter-option" id="spanNameR"><?= $groups[0]['name']; ?></span>&nbsp;<i class="fa fa-caret-down"></i>
                                                            </button>
                                                                <ul class="dropdown-menu" role="menu" style="max-height: 323px; overflow-y: auto; min-height: 60px;">
                                                                    <?php
                                                                        foreach($groups as $value)
                                                                        {
                                                                            echo '<li rel="'.$value['id'].'" onClick="setResult('.$value['id'].',\''.$value['name'].'\');"><a tabindex="1" href="#" class="">'.$value['name'].'</a></li>';
                                                                        }
                                                                    ?>
                                                                </ul>
                                                        </div>
                                                    </div>
                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                    
                                    <table id="games" class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th style="text-align: center"><strong>Fecha</strong></th>
                                                <th style="text-align: center"><strong>Puntos</strong></th>
                                                <th style="text-align: center"><strong>Ficha</strong></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                    
                                            <?php
                                            $cantPuntos = 0;
                                            foreach($results as $r)
                                            {
                                                $cantPuntos += $r['points'];
                                                echo '<tr>';
                                                echo '<td>'.$r['dtDescription'].'</td>';
                                                echo '<td style="text-align: center">'.$r['points'].'</td>';
                                                echo '<td style="text-align: center">
                                                        <i class="fa fa-search" style="cursor:pointer;" title="ver ficha" onclick="moredetails('.$r['dtId'].',\''.$r['dtDescription'].'\')" ></i>
                                                      </td>';
                                                echo '</tr>';
                                            }
                                            
                                            ?>
                                            <tr>
                                                <td style="text-align: right;"><strong>Total:</strong></td>
                                                <td style="text-align: center; background-color: #51A351;"><strong><?php echo $cantPuntos; ?></strong></td>
                                                <td style="text-align: center">-</td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    
                                    <?php
                                    }
                                }
                            }
                            ?>
				    
                    </div>
                </section>
                <!--#end region fecha-->
                
            </div>
            

            <div class="col-md-6">

                <?php
                if($results > 0)
                {
                ?>
                 <section class="widget">
                    <header>
                        <h4>
                            <i class="fa fa-bars"></i>
                            La Fecha 
                            <small>
                                detallada <span id="title_date"></span>
                            </small> 
                        </h4>
                    </header>
                    <div class="body text-align-center" >
                        <table id="table_team" class="table table-striped">
                            <thead>
                                <tr>
                                    <th colspan="2" style="text-align: center"><strong>Local</strong></th>
                                    <th colspan="2" style="text-align: center"><strong>Visita</strong></th>
                                    <th style="text-align: center"><strong>Jugada</strong></th>
                                    <th style="text-align: center"><strong>Res.</strong></th>
                                    <th style="text-align: center; width: 35px;"> - </th>
                                </tr>
                            </thead>
                            <tbody id="table_team_game">
                                
                            </tbody>
                        </table>
                        <div id="error_msj" style="color: red"></div>
                    </div>
                 </section>
                <?php
                }
                ?>
            </div>
</div>